import 'package:flutter/material.dart';

class RadioGroup extends StatefulWidget {
  final Map data;
  final String defaulVlue;
  final Function(String) onChange;

  RadioGroup({@required this.data, @required this.defaulVlue, @required this.onChange});
  @override
  _RadioGroupState createState() {
    return _RadioGroupState();
  }
}
class _RadioGroupState extends State<RadioGroup> {
  String groupValue;
  @override
  void initState() {
    super.initState();
    this.groupValue = widget.defaulVlue;
  }

  void setVal(String val) {
    if(this.groupValue != val) {
      setState(() {
        this.groupValue = val;
        widget.onChange(val);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets = [];
    widget.data.forEach((val, text){
      widgets.add(
        Row(
          children: <Widget>[
            Radio<String>(
              value: val,
              groupValue: this.groupValue,
              onChanged: this.setVal,
            ),
            GestureDetector(
              child: Text(text),
              onTap: (){this.setVal(val);}
            )
          ],
        )
      );
    });
    return Row(
      children: widgets,
    );
  }
}